package gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
 
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Vector;
 
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

import code.KeyStoreManager;
import code.Language;
import code.Settings;


public class PersonalListCertificate extends JPanel {
	
	Color c= new Color(0x80, 0x80, 0xFF);
	private HashMap labels;

	private static final long serialVersionUID = 1L;
	private JScrollPane jScrollPane = null;
	private JTable jTable = null;
	private JLabel jLabel = null;
	ATOCOmain atocoframe;
	private KeyStoreManager ksm;
	private Settings s;
	private JLabel jLabel1 = null;
	/**
	 * This is the default constructor
	 */
	public PersonalListCertificate(ATOCOmain atocoframe) {
		super();
		Language l = new Language();
		s= new Settings();
		labels = l.changeLanguage((String) s.global.get("language"), "PersonalListCertificate", (String) s.global.get("installPath"));

		// Every Keystore operation must use this one.
		ksm = new KeyStoreManager(
				(String)Settings.global.get("personalKeyStoreFile"), 
				(String)Settings.global.get("personalKeyStorePassword"));
		if (ksm.getKeyStore() == null) {
        	System.out.println("Keystore file not found. Creating one.");
        	ksm.createKeyStore(
        			(String)Settings.global.get("personalKeyStoreFile"), 
    				(String)Settings.global.get("personalKeyStorePassword"));
        }
		initialize();
		this.atocoframe=atocoframe;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		jLabel1 = new JLabel();
		jLabel1.setBounds(new Rectangle(532, 114, 127, 39));
		jLabel1.setText((String) labels.get("jLabel1"));
		jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				atocoframe.contentPane(new PersonalViewCertificate(atocoframe,
						jTable.getValueAt(jTable.getSelectedRow(), 0).toString(), passclass(), "PersonalListCert"));
			}
		});
		jLabel = new JLabel();
		jLabel.setBounds(new Rectangle(61, 17, 127, 16));
		jLabel.setText((String) labels.get("jLabel"));
		jLabel.setForeground(c);
		jLabel.setFont(new java.awt.Font("Dialog", Font.BOLD, 15));
		this.setSize(764, 417);
		this.setLayout(null);
		this.add(getJScrollPane(), null);
		this.setBackground(Color.WHITE);
		this.add(jLabel, null);
		this.add(jLabel1, null);
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(60, 75, 441, 281));
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTable() {
		if (jTable == null) {
			
			Vector<Object> rowData = new Vector<Object>();
			
			HashSet<String> aliases = ksm.listAllCerts();
			Iterator it = aliases.iterator();
			while(it.hasNext()) {
				String al = (String) it.next();
				Vector<Object> row = new Vector<Object>();
				row.add(al);
				row.add(
						ksm.getCertificate(al).toString().substring(
								ksm.getCertificate(al).toString().indexOf("Subject:"),
								ksm.getCertificate(al).toString().indexOf("Signature Algo")
								));
				System.out.println(ksm.getCertificate(al).toString());
				rowData.add(row);
			}
			
			aliases = ksm.listAllKeys();
			it = aliases.iterator();
			while(it.hasNext()) {
				String al = (String) it.next();
				if (ksm.getPublicKey(al) == null) {
	        		// No certificate entry so it must be secretKey	
				} else {
					if (!ksm.getPublicKey(al).equals(null)) {
						// Has certificate entry so it must be KeyPair
						Vector<Object> row = new Vector<Object>();
						row.add(al);
						row.add(
								ksm.getCertificate(al).toString().substring(
										ksm.getCertificate(al).toString().indexOf("Subject:"),
										ksm.getCertificate(al).toString().indexOf("Signature Algo")
										));
						System.out.println(ksm.getCertificate(al).toString());
						rowData.add(row);
					}
				}
			}
			Vector<String> columnNames = new Vector<String>();
			columnNames.add((String) labels.get("tab1"));
			columnNames.add((String) labels.get("tab2"));
			
			if (!rowData.isEmpty()) {
				DefaultTableModel model = new DefaultTableModel(rowData, columnNames);
			
			jTable = new JTable(model){
				//  Returning the Class of each column will allow different
				//  renderers to be used based on Class
				public Class getColumnClass(int column)
				{
					return getValueAt(0, column).getClass();
				}
			};
			} else {
				jScrollPane.setVisible(false);
			}

		}
		return jTable;
	}

	public PersonalListCertificate passclass() {
		return this;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
